/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author David Life
 */
public class Logica extends HttpServlet {
    // esta es la matriz con las imagenes de las banderas
    public   String [][] imagenes;
     int i;
     int j;
    // matriz con las imagenes en blanco mostradas al iniciar el juego
     public   String [][] vacio= new String[][]{
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                 };
      public   String [][] vacio1= new String[][]{
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg"},
                    {"Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg", "Images/0.jpg","Images/0.jpg"},
                 };

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
      //  PrintWriter out = response.getWriter();
        
         // i y j son parametros capturaso desde el jsp que nos dice en que posicion del tablero se ha hecho click
           try {
               // coordenadas del clik sobre la tabla
               i = Integer.parseInt(request.getParameter("i"));
               j = Integer.parseInt(request.getParameter("j"));
               // guardo en la matriz vacia la imagen de la bandera en las coordenadas
             
            vacio[i][j]=imagenes[i][j];
            for(int a=0;a<vacio.length;a++){
              for(int b=0;b<vacio.length;b++){
                   if(!vacio[a][b].equals(vacio[i][j])){
                    vacio[a][b]=vacio1[i][j];
                      
                    }else{
                 
                    }

                    
                }
             }
        } catch (Exception e) {
        }

        // arreglo de banderas a mostrar cuando se ha ce click sobre una de las casillas del tablero de juegos
         imagenes = new String[][]{
                    {"Images/1.jpg", "Images/2.jpg", "Images/3.jpg", "Images/4.jpg", "Images/5.jpg","Images/6.jpg"},
                    {"Images/7.jpg", "Images/8.jpg", "Images/9.jpg", "Images/10.jpg", "Images/11.jpg", "Images/12.jpg"},
                    {"Images/13.jpg", "Images/14.jpg", "Images/15.jpg", "Images/16.jpg", "Images/17.jpg","Images/18.jpg"},
                    {"Images/1.jpg", "Images/2.jpg", "Images/3.jpg", "Images/4.jpg", "Images/5.jpg","Images/6.jpg"},
                    {"Images/7.jpg", "Images/8.jpg", "Images/9.jpg", "Images/10.jpg", "Images/11.jpg", "Images/12.jpg"},
                    {"Images/13.jpg", "Images/14.jpg", "Images/15.jpg", "Images/16.jpg", "Images/17.jpg","Images/18.jpg"},
                };
      request.setAttribute("imagenes", imagenes);
        request.setAttribute("vacio", vacio);
        request.getRequestDispatcher("/TableroDeJuego.jsp").include(request, response);
        ;
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

   

    private void todos(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

   

   

}
